// var commonCityData = require('../../utils/city.js')
//获取应用实例
var app = getApp()
Page({
  data: {
    provinces:[],
    citys:[],
    districts:[],
    selProvince:'请选择',
    selCity:'请选择',
    selDistrict:'请选择',
    selProvinceIndex:0,
    selCityIndex:0,
    selDistrictIndex:0,
    said:0,
    addressData:null,
    cityList: [],
    CountyList:[]
  },
  bindCancel:function () {
    wx.navigateBack({})
  },
  bindSave: function(e) {
    var that = this;
    var consignee = e.detail.value.consignee;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;
    var zipcode = e.detail.value.zipcode;

    if (consignee == ""){
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel:false
      })
      return
    }
    if (mobile == ""){
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel:false
      })
      return
    }
    if (this.data.selProvince == "请选择"){
      wx.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel:false
      })
      return
    }
    if (this.data.selCity == "请选择"){
      wx.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel:false
      })
      return
    }
    if (this.data.selDistrict == "请选择"){
      wx.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel:false
      })
      return
    }
    var districtId = parseInt(this.data.selDistrictIndex)
    if (address == ""){
      wx.showModal({
        title: '提示',
        content: '请填写详细地址',
        showCancel:false
      })
      return
    }
    if (zipcode == ""){
      wx.showModal({
        title: '提示',
        content: '请填写邮编',
        showCancel:false
      })
      return
    }
    var apiAddoRuPDATE = "add";
    var apiAddid = that.data.said;
    if (apiAddid) {
      apiAddoRuPDATE = "edit";
    } else {
      apiAddid = 0;
    }
    wx.request({
      url: app.globalData.host + '/api/user/'+apiAddoRuPDATE+'shipaddress',
      data: {
        uid: app.globalData.userInfo.Uid,
        said: apiAddid,
        regionid: districtId,
        consignee: consignee,
        address:address,
        mobile:mobile,
        zipcode: zipcode,
        isDefault:1
      },
      success: function(res) {
        if (res.data.Code != 0) {
          // 登录错误 
          wx.hideLoading();
          wx.showModal({
            title: '失败',
            content: res.data.Message,
            showCancel:false
          })
          return;
        }
        // 跳转到结算页面
        wx.navigateBack({})
      }
    })
  },

  /**
   * 获取省接口
   */
  getProvince:function(e){
    var that =this;
    wx.request({
      url: app.globalData.host + '/api/Tool/ProvinceList',
      success: function (res) {
        var provincesArray = JSON.parse(res.data.Data)
        var pinkArray = [];
        for (var i = 0; i < provincesArray.length; i++) {
          pinkArray.push(provincesArray[i].name);
        }
        that.setData({
          provinces: pinkArray
        })
      }
    })
  },
  //省值选择变化
  bindPickerProvinceChange: function (event) {
    var selIterm = this.data.provinces[event.detail.value]
    var provinceId = parseInt(event.detail.value) + 1
    this.setData({
      selProvince: selIterm,
      selProvinceIndex: provinceId,
      selCity: '请选择',
      selDistrict: '请选择'
    })
    this.getCityList(provinceId)//初始化市数据
  },
  /**
 * 获取市接口
 */
  getCityList: function (provinceid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/CityList?provinceId=' + provinceid,
      success: function (res) {
        var cityList = JSON.parse(res.data.Data)
        var pinkCitys = [];
        for (var i = 0; i < cityList.length; i++) {
          pinkCitys.push(cityList[i].name);
        }
        that.setData({
          citys: pinkCitys,
          cityList: cityList
        })
      }
    })
  },
  //市值选择
  bindPickerCityChange: function (event) {
    var cityList = this.data.cityList
    var selIterm = this.data.citys[event.detail.value]
    for (var i = 0; i < cityList.length;i++){
      if (selIterm == cityList[i].name){
        var citysId = cityList[i].id
      }
    }
    this.setData({
      selCity: selIterm,
      selCityIndex: citysId,
      selDistrict: '请选择'
    })
    this.getCountyList(citysId)
  },
  /**
* 当点击获取县地区
*/
  getCountyList: function (citysid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/CityList?provinceId=' + citysid,
      success: function (res) {
        var CountyList = JSON.parse(res.data.Data)
        var pinkCounty = [];
        for (var i = 0; i < CountyList.length; i++) {
          pinkCounty.push(CountyList[i].name);
        }
        that.setData({
          districts: pinkCounty,
          CountyList: CountyList
        })
      }
    })
  },

  bindPickerChange:function (event) {
    var CountyList = this.data.CountyList
    var selIterm = this.data.districts[event.detail.value]
    for (var i = 0; i < CountyList.length; i++) {
      if (selIterm == CountyList[i].name) {
        var DistrictId = CountyList[i].id
      }
    }
    this.setData({
      selDistrict:selIterm,
      selDistrictIndex: DistrictId
    })
  },
  onLoad: function (e) {
    var that = this;
    that.getProvince()
    var id = e.said;
    if (id&&id>0) {
      // 初始化原数据
      wx.showLoading();
      wx.request({
        url: app.globalData.host + '/api/user/GetShipAddress',
        data: {
          uid: app.globalData.userInfo.Uid,
          said: id
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data.Code == 0) {
            that.setData({
              said:id,
              addressData: res.data.Data,
              selProvince: res.data.Data.ProvinceName,
              selCity: res.data.Data.CityName,
              selDistrict: res.data.Data.CountyName,
            });
            return;
          } else {
            wx.showModal({
              title: '提示',
              content: '无法获取快递地址数据',
              showCancel: false
            })
          }
        }
      })
    }
  },

  deleteAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/user/DelShipAddress',
            data: {
              uid: app.globalData.userInfo.Uid,
              said: id
            },
            success: (res) => {
              wx.navigateBack({})
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  }
})
